package com.shixu.framework.plugins.weixin.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.shixu.framework.plugins.weixin.util.HttpKit;



/**
 * 用户操作接口
 * @author
 */
public class GroupService {

	private static final String GROUP_CREATE_URI = "https://api.weixin.qq.com/cgi-bin/groups/create?access_token=";
	private static final String GROUP_GET_URI = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token=";
	private static final String GROUP_GETID_URI = "https://api.weixin.qq.com/cgi-bin/groups/getid?access_token=";
	private static final String GROUP_UPDATE_URI = "https://api.weixin.qq.com/cgi-bin/groups/update?access_token=";
	private static final String GROUP_MEMBERS_UPDATE_URI = "https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=";

	/**
	 * 创建分组
	 * @param accessToken
	 * @param name 分组名字（30个字符以内）
	 * @return
	 * @throws Exception
	 */
	public JSONObject create(String accessToken, String name) throws Exception {
		Map<String,Object> group = new HashMap<String,Object>();
        Map<String,Object> nameObj = new HashMap<String,Object>();
        nameObj.put("name", name);
        group.put("group", nameObj);
        String post = JSONObject.fromObject(group).toString();
    	String reslut = HttpKit.post(GROUP_CREATE_URI.concat(accessToken), post);
    	if(StringUtils.isNotEmpty(reslut)){
    		return JSONObject.fromObject(reslut);
    	}
        return null;
	}
	
	/**
	 * 查询所有分组
	 * @param accessToken
	 * @return
	 * @throws Exception
	 */
	public JSONObject get(String accessToken) throws Exception {
    	String reslut = HttpKit.get(GROUP_GET_URI.concat(accessToken));
    	if(StringUtils.isNotEmpty(reslut)){
    		return JSONObject.fromObject(reslut);
    	}
        return null;
	}
	
	/**
	 * 查询用户所在分组
	 * @param accessToken
	 * @param openid 用户的OpenID
	 * @return
	 * @throws Exception
	 */
	public JSONObject membersIn(String accessToken,String openid) throws Exception {
    	String reslut = HttpKit.post(GROUP_GETID_URI.concat(accessToken),"{\"openid\":\""+openid+"\"}");
    	if(StringUtils.isNotEmpty(reslut)){
    		return JSONObject.fromObject(reslut);
    	}
        return null;
	}
	
	/**
	 * 修改分组名
	 * @param accessToken
	 * @param id 分组id，由微信分配
	 * @param name 分组名字（30个字符以内）
	 * @return
	 * @throws Exception
	 */
	public JSONObject updateName(String accessToken,String id,String name) throws Exception {
		Map<String,Object> group = new HashMap<String,Object>();
        Map<String,Object> nameObj = new HashMap<String,Object>();
        nameObj.put("name", name);
        nameObj.put("id", id);
        group.put("group", nameObj);
        String post = JSONObject.fromObject(group).toString();
    	String reslut = HttpKit.post(GROUP_UPDATE_URI.concat(accessToken), post);
    	if(StringUtils.isNotEmpty(reslut)){
    		return JSONObject.fromObject(reslut);
    	}
        return null;
	}
	
	/**
	 * 移动用户分组
	 * @param accessToken
	 * @param openid 用户唯一标识符
	 * @param to_groupid 分组id
	 * @return
	 * @throws Exception
	 */
	public JSONObject membersMove(String accessToken,String openid,String to_groupid) throws Exception {
    	String reslut = HttpKit.post(GROUP_MEMBERS_UPDATE_URI.concat(accessToken), "{\"openid\":\""+openid+"\",\"to_groupid\":"+to_groupid+"}");
    	if(StringUtils.isNotEmpty(reslut)){
    		return JSONObject.fromObject(reslut);
    	}
        return null;
	}
	
	
	/**
	 * 测试
	 * @param args
	 * @throws Exception 
	 * @throws IOException
	 * @throws InterruptedException 
	 * @throws ExecutionException 
	 */
	public static void main(String[] args) throws Exception{
		//实例化操作类
		GroupService group = new GroupService();		
		//定义返回值
		String result = null;
		
		String access_token = "fWbIL3Gy1pPkF9aNIXWzV9CreOH9wxV8GELTchOBvDvFv2jFYB9RyKmMr9phXYSYYctufMK6n3IahwRKVC7h0FjZqlKxH0A_nEo8HI-6nSU";
		String openid = "o9QgNuDF7-3lY4nH1yuLRF-jRhvg";
		
		/* 创建分组 */
//		result = group.create(access_token, "测试创建分组").toString();
//	    System.out.println("测试创建分组 ----"+result); 
		
		/* 查询所有分组 */
//		result = group.get(access_token).toString();
//		System.out.println("查询所有分组----"+result);
		
		/* 查询用户所在分组 */
//		result = group.membersIn(access_token, openid).toString();
//		System.out.println("查询用户所在分组----"+result);
		
		/* 修改分组名 */
//		result = group.updateName(access_token, "100", "测试修改分组名").toString();
//		System.out.println("修改分组名----"+result);
		
		/* 移动用户分组 */
//		result = group.membersMove(access_token, openid, "0").toString();
//		System.out.println("修改分组名----"+result);
	    
	  
	}  
	
}